<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python高级 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>分组 | python高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../day15/07.连接.html" />
    
    
    <link rel="prev" href="../day15/05.聚合函数.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="5.6"
        data-chapter-title="分组"
        data-filepath="day15/06.分组.md"
        data-basepath=".."
        data-revision="Wed Sep 26 2018 22:46:13 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="day11/00.readme.html">
            
                
                    <a href="../day11/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Python语法高级专题1
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="day11/01.GIL.html">
            
                
                    <a href="../day11/01.GIL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        GIL问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="day11/02.copy.html">
            
                
                    <a href="../day11/02.copy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        赋值-浅拷贝-深拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="day11/03.import.html">
            
                
                    <a href="../day11/03.import.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="day11/04.review.html">
            
                
                    <a href="../day11/04.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="day12/00.readme.html">
            
                
                    <a href="../day12/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Python语法高级专题2
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="day12/01.mro.html">
            
                
                    <a href="../day12/01.mro.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        多继承和MRO顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="day12/02.property.html">
            
                
                    <a href="../day12/02.property.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        property属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="day12/03.magic.html">
            
                
                    <a href="../day12/03.magic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        魔法属性和方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="day12/04.with.html">
            
                
                    <a href="../day12/04.with.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        with管理上下文
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="day12/05.review.html">
            
                
                    <a href="../day12/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="day13/00.readme.html">
            
                
                    <a href="../day13/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Python语法之装饰器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="day13/01.closure.html">
            
                
                    <a href="../day13/01.closure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="day13/02.decorator.html">
            
                
                    <a href="../day13/02.decorator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="day13/03.review.html">
            
                
                    <a href="../day13/03.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="day14/00.readme.html">
            
                
                    <a href="../day14/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        MySQL数据库基本操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="day14/01.数据库概述.html">
            
                
                    <a href="../day14/01.数据库概述.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        数据库概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="day14/02.数据类型和约束.html">
            
                
                    <a href="../day14/02.数据类型和约束.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        数据完整性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="day14/03.Navicat.html">
            
                
                    <a href="../day14/03.Navicat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        图形化客户端Navicat
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="day14/04.命令行客户端.html">
            
                
                    <a href="../day14/04.命令行客户端.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        命令行客户端SQL的使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.4.1" data-path="day14/05.sql_con.html">
            
                
                    <a href="../day14/05.sql_con.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.1.</b>
                        
                        登录和退出数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.2" data-path="day14/07.sql_table_struct.html">
            
                
                    <a href="../day14/07.sql_table_struct.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.2.</b>
                        
                        表结构的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.3" data-path="day14/06.sql_database.html">
            
                
                    <a href="../day14/06.sql_database.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.3.</b>
                        
                        数据库的操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4.4" data-path="day14/08.表数据操作.html">
            
                
                    <a href="../day14/08.表数据操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.4.</b>
                        
                        表数据的操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="day14/09.review.html">
            
                
                    <a href="../day14/09.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        知识总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="day15/00.readme.html">
            
                
                    <a href="../day15/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        MYSQL数据库进阶操作
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="day15/01.练习.html">
            
                
                    <a href="../day15/01.练习.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        练习强化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="day15/02.where条件.html">
            
                
                    <a href="../day15/02.where条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        条件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="day15/03.排序查询.html">
            
                
                    <a href="../day15/03.排序查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        排序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="day15/04.分页查询.html">
            
                
                    <a href="../day15/04.分页查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="day15/05.聚合函数.html">
            
                
                    <a href="../day15/05.聚合函数.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        聚合函数
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.6" data-path="day15/06.分组.html">
            
                
                    <a href="../day15/06.分组.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        分组
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="day15/07.连接.html">
            
                
                    <a href="../day15/07.连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="day15/08.自连接.html">
            
                
                    <a href="../day15/08.自连接.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="day15/09.子查询.html">
            
                
                    <a href="../day15/09.子查询.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        子查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.10" data-path="day15/10.总结.html">
            
                
                    <a href="../day15/10.总结.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.10.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="day16/readme.html">
            
                
                    <a href="../day16/readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        MYSQL数据库编程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="day16/data.html">
            
                
                    <a href="../day16/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        数据准备
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="day16/sql_code.html">
            
                
                    <a href="../day16/sql_code.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        SQL演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="day16/dbdesign.html">
            
                
                    <a href="../day16/dbdesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="day16/pymysql.html">
            
                
                    <a href="../day16/pymysql.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        数据库编程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="day16/zongjie.html">
            
                
                    <a href="../day16/zongjie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="day17/00.readme.html">
            
                
                    <a href="../day17/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        MySQL数据库高级用法
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="day17/01.view.html">
            
                
                    <a href="../day17/01.view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="day17/02.transaction.html">
            
                
                    <a href="../day17/02.transaction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        事务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="day17/03.sql_index.html">
            
                
                    <a href="../day17/03.sql_index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="day17/04.user.html">
            
                
                    <a href="../day17/04.user.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        用户管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="day17/05.master_slave.html">
            
                
                    <a href="../day17/05.master_slave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        主从配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="day17/06.review.html">
            
                
                    <a href="../day17/06.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.6.</b>
                        
                        总结
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="day18/00.readme.html">
            
                
                    <a href="../day18/00.readme.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        miniweb框架
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="day18/01.框架是什么.html">
            
                
                    <a href="../day18/01.框架是什么.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        框架概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="day18/02.实现框架.html">
            
                
                    <a href="../day18/02.实现框架.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        实现一个简单的框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="day18/03.模板.html">
            
                
                    <a href="../day18/03.模板.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        模板替换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="day18/04.路由.html">
            
                
                    <a href="../day18/04.路由.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        路由概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="day18/05.显示股票数据.html">
            
                
                    <a href="../day18/05.显示股票数据.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        股票收藏夹
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="day18/05.review.html">
            
                
                    <a href="../day18/05.review.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        复习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="6-&#x5206;&#x7EC4;">6. &#x5206;&#x7EC4;</h1>
<h2 id="61-&#x5B66;&#x4E60;&#x76EE;&#x6807;">6.1 &#x5B66;&#x4E60;&#x76EE;&#x6807;</h2>
<ol>
<li>&#x80FD;&#x591F;&#x8BF4;&#x51FA;&#x5206;&#x7EC4;&#x7684;&#x76EE;&#x7684;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528; group by &#x5B8C;&#x6210;&#x5BF9;&#x6570;&#x636E;&#x7684;&#x5206;&#x7EC4;</li>
<li>&#x80FD;&#x591F;&#x8BF4;&#x51FA; group_concat &#x51FD;&#x6570;&#x7684;&#x4F5C;&#x7528;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528;&#x805A;&#x5408;&#x51FD;&#x6570;&#x5B8C;&#x6210;&#x5BF9;&#x5206;&#x7EC4;&#x7ED3;&#x679C;&#x7684;&#x7EDF;&#x8BA1;</li>
<li>&#x80FD;&#x591F;&#x4F7F;&#x7528; having &#x5B8C;&#x6210;&#x5BF9;&#x5206;&#x7EC4;&#x7ED3;&#x679C;&#x7684;&#x6761;&#x4EF6;&#x8FC7;&#x6EE4;</li>
</ol>
<hr>
<h2 id="62-&#x5206;&#x7EC4;&#x7528;&#x6CD5;">6.2 &#x5206;&#x7EC4;&#x7528;&#x6CD5;</h2>
<blockquote>
<p>&#x4EC0;&#x4E48;&#x662F;&#x5206;&#x7EC4;
    &#x6240;&#x8C13;&#x7684;&#x5206;&#x7EC4;&#x5C31;&#x662F;&#x5C06;&#x4E00;&#x4E2A;&#x201C;&#x6570;&#x636E;&#x96C6;&#x201D;&#x5212;&#x5206;&#x6210;&#x82E5;&#x5E72;&#x4E2A;&#x201C;&#x5C0F;&#x533A;&#x57DF;&#x201D;&#xFF0C;&#x7136;&#x540E;&#x9488;&#x5BF9;&#x82E5;&#x5E72;&#x4E2A;&#x201C;&#x5C0F;&#x533A;&#x57DF;&#x201D;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x5904;&#x7406;&#x3002;</p>
</blockquote>
<h3 id="621-group-by&#x5206;&#x7EC4;">6.2.1 group by&#x5206;&#x7EC4;</h3>
<blockquote>
<p>&#x4F7F;&#x7528;&#x7279;&#x70B9;</p>
<ol>
<li>group by&#x7684;&#x542B;&#x4E49;:&#x5C06;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x6309;&#x7167;1&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#xFF0C;&#x5B57;&#x6BB5;&#x503C;&#x76F8;&#x540C;&#x7684;&#x4E3A;&#x4E00;&#x7EC4;</li>
<li>group by&#x53EF;&#x7528;&#x4E8E;&#x5355;&#x4E2A;&#x5B57;&#x6BB5;&#x5206;&#x7EC4;&#xFF0C;&#x4E5F;&#x53EF;&#x7528;&#x4E8E;&#x591A;&#x4E2A;&#x5B57;&#x6BB5;&#x5206;&#x7EC4;</li>
</ol>
</blockquote>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> * <span class="hljs-keyword">from</span> students;</span>
+<span class="hljs-comment">----+-----------+------+--------+--------+--------+-----------+</span>
| id | name      | age  | height | gender | cls_id | is_delete |
+<span class="hljs-comment">----+-----------+------+--------+--------+--------+-----------+</span>
|  1 | &#x5C0F;&#x660E;      |   18 | 180.00 | &#x5973;     |      1 |           |
|  2 | &#x5C0F;&#x6708;&#x6708;    |   18 | 180.00 | &#x5973;     |      2 |          |
|  3 | &#x5F6D;&#x4E8E;&#x664F;    |   29 | 185.00 | &#x7537;     |      1 |           |
|  4 | &#x5218;&#x5FB7;&#x534E;    |   59 | 175.00 | &#x7537;     |      2 |          |
|  5 | &#x9EC4;&#x84C9;      |   38 | 160.00 | &#x5973;     |      1 |           |
|  6 | &#x51E4;&#x59D0;      |   28 | 150.00 | &#x4FDD;&#x5BC6;   |      2 |          |
|  7 | &#x738B;&#x7956;&#x8D24;    |   18 | 172.00 | &#x5973;     |      1 |          |
|  8 | &#x5468;&#x6770;&#x4F26;    |   36 |   NULL | &#x7537;     |      1 |           |
|  9 | &#x7A0B;&#x5764;      |   27 | 181.00 | &#x7537;     |      2 |           |
| 10 | &#x5218;&#x4EA6;&#x83F2;    |   25 | 166.00 | &#x5973;     |      2 |           |
| 11 | &#x91D1;&#x661F;      |   33 | 162.00 | &#x4E2D;&#x6027;   |      3 |          |
| 12 | &#x9759;&#x9999;      |   12 | 180.00 | &#x5973;     |      4 |           |
| 13 | &#x5468;&#x6770;      |   34 | 176.00 | &#x5973;     |      5 |           |
| 14 | &#x90ED;&#x9756;      |   12 | 170.00 | &#x7537;     |      4 |           |
+<span class="hljs-comment">----+-----------+------+--------+--------+--------+-----------+</span>

<span class="hljs-operator"><span class="hljs-keyword">select</span> gender <span class="hljs-keyword">from</span> students <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> gender;</span>
+<span class="hljs-comment">--------+</span>
| gender |
+<span class="hljs-comment">--------+</span>
| &#x7537;     |
| &#x5973;     |
| &#x4E2D;&#x6027;   |
| &#x4FDD;&#x5BC6;   |
+<span class="hljs-comment">--------+</span>
</code></pre>
<p>&#x6839;&#x636E;gender&#x5B57;&#x6BB5;&#x6765;&#x5206;&#x7EC4;&#xFF0C;gender&#x5B57;&#x6BB5;&#x7684;&#x5168;&#x90E8;&#x503C;&#x6709;4&#x4E2A;&apos;&#x7537;&apos;,&apos;&#x5973;&apos;,&apos;&#x4E2D;&#x6027;&apos;,&apos;&#x4FDD;&#x5BC6;&apos;&#xFF0C;&#x6240;&#x4EE5;&#x5206;&#x4E3A;&#x4E86;4&#x7EC4;</p>
<blockquote>
<p>&#x5728;&#x73B0;&#x5728;MySQL&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;, select &#x540E;&#x7684;&#x5B57;&#x6BB5;&#x53EA;&#x80FD;&#x51FA;&#x73B0;&#x5728;&#x4EE5;&#x4E0B;&#x4E24;&#x79CD;&#x60C5;&#x51B5;:</p>
<ul>
<li>&#x5728;group by&#x540E;&#x51FA;&#x73B0;&#x8FC7;</li>
<li>&#x5728;&#x805A;&#x5408;&#x51FD;&#x6570;&#x4E2D;&#x51FA;&#x73B0;</li>
</ul>
</blockquote>
<h2 id="622-group-by--groupconcat">6.2.2 group by + group_concat()</h2>
<blockquote>
<p>group_concat(&#x5B57;&#x6BB5;&#x540D;)&#x6839;&#x636E;&#x5206;&#x7EC4;&#x7ED3;&#x679C;&#xFF0C;&#x4F7F;&#x7528;group_concat()&#x6765;&#x653E;&#x7F6E;&#x6BCF;&#x4E00;&#x4E2A;&#x5206;&#x7EC4;&#x4E2D;&#x67D0;&#x5B57;&#x6BB5;&#x7684;&#x96C6;&#x5408;</p>
</blockquote>
<pre><code class="lang-sql"><span class="hljs-operator"><span class="hljs-keyword">select</span> gender <span class="hljs-keyword">from</span> students <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> gender;</span>
+<span class="hljs-comment">--------+</span>
| gender |
+<span class="hljs-comment">--------+</span>
| &#x7537;     |
| &#x5973;     |
| &#x4E2D;&#x6027;   |
| &#x4FDD;&#x5BC6;   |
+<span class="hljs-comment">--------+</span>

<span class="hljs-operator"><span class="hljs-keyword">select</span> gender,<span class="hljs-keyword">group_concat</span>(<span class="hljs-keyword">name</span>) <span class="hljs-keyword">from</span> students <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> gender;</span>
+<span class="hljs-comment">--------+-----------------------------------------------------------+</span>
| gender | group_concat(name)                                        |
+<span class="hljs-comment">--------+-----------------------------------------------------------+</span>
| &#x7537;     | &#x5F6D;&#x4E8E;&#x664F;,&#x5218;&#x5FB7;&#x534E;,&#x5468;&#x6770;&#x4F26;,&#x7A0B;&#x5764;,&#x90ED;&#x9756;                                 |
| &#x5973;     | &#x5C0F;&#x660E;,&#x5C0F;&#x6708;&#x6708;,&#x9EC4;&#x84C9;,&#x738B;&#x7956;&#x8D24;,&#x5218;&#x4EA6;&#x83F2;,&#x9759;&#x9999;,&#x5468;&#x6770;                        |
| &#x4E2D;&#x6027;   | &#x91D1;&#x661F;                                                       |
| &#x4FDD;&#x5BC6;   | &#x51E4;&#x59D0;                                                       |
+<span class="hljs-comment">--------+-----------------------------------------------------------+</span>


<span class="hljs-operator"><span class="hljs-keyword">select</span> gender,<span class="hljs-keyword">group_concat</span>(<span class="hljs-keyword">id</span>) <span class="hljs-keyword">from</span> students <span class="hljs-keyword">group</span> <span class="hljs-keyword">by</span> gender;</span>
+<span class="hljs-comment">--------+------------------+</span>
| gender | group_concat(id) |
+<span class="hljs-comment">--------+------------------+</span>
| &#x7537;     | 3,4,8,9,14       |
| &#x5973;     | 1,2,5,7,10,12,13 |
| &#x4E2D;&#x6027;   | 11               |
| &#x4FDD;&#x5BC6;   | 6                |
+<span class="hljs-comment">--------+------------------+</span>
</code></pre>
<h3 id="623-group-by--&#x805A;&#x5408;&#x51FD;&#x6570;">6.2.3 group by + &#x805A;&#x5408;&#x51FD;&#x6570;</h3>
<p>&#x901A;&#x8FC7;group_concat()&#x7684;&#x542F;&#x53D1;&#xFF0C;&#x6211;&#x4EEC;&#x65E2;&#x7136;&#x53EF;&#x4EE5;&#x7EDF;&#x8BA1;&#x51FA;&#x6BCF;&#x4E2A;&#x5206;&#x7EC4;&#x7684;&#x67D0;&#x5B57;&#x6BB5;&#x7684;&#x503C;&#x7684;&#x96C6;&#x5408;&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x96C6;&#x5408;&#x51FD;&#x6570;&#x6765;&#x5BF9;&#x8FD9;&#x4E2A;<code>&#x503C;&#x7684;&#x96C6;&#x5408;</code>&#x505A;&#x4E00;&#x4E9B;&#x64CD;&#x4F5C;</p>
<blockquote>
<p>&#x805A;&#x5408;&#x51FD;&#x6570;&#x5728;&#x548C;group by&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x7684;&#x65F6;&#x5019; &#x7EDF;&#x8BA1;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x6BCF;&#x4E00;&#x4E2A;&#x5206;&#x7EC4;&#x3002;</p>
</blockquote>
<pre><code class="lang-sql">select gender,group_concat(age) from students group by gender;
+--------+----------------------+
| gender | group_concat(age)    |
+--------+----------------------+
| &#x7537;     | 29,59,36,27,12       |
| &#x5973;     | 18,18,38,18,25,12,34 |
| &#x4E2D;&#x6027;   | 33                   |
| &#x4FDD;&#x5BC6;   | 28                   |
+--------+----------------------+


&#x5206;&#x522B;&#x7EDF;&#x8BA1;&#x6027;&#x522B;&#x4E3A;&#x7537;/&#x5973;&#x7684;&#x4EBA;&#x5E74;&#x9F84;&#x5E73;&#x5747;&#x503C;
select gender,avg(age) from students group by gender;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| &#x7537;     |  32.6000 |
| &#x5973;     |  23.2857 |
| &#x4E2D;&#x6027;   |  33.0000 |
| &#x4FDD;&#x5BC6;   |  28.0000 |
+--------+----------+

&#x5206;&#x522B;&#x7EDF;&#x8BA1;&#x6027;&#x522B;&#x4E3A;&#x7537;/&#x5973;&#x7684;&#x4EBA;&#x7684;&#x4E2A;&#x6570;
select gender,count(*) from students group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| &#x7537;     |        5 |
| &#x5973;     |        7 |
| &#x4E2D;&#x6027;   |        1 |
| &#x4FDD;&#x5BC6;   |        1 |
+--------+----------+
</code></pre>
<h3 id="624-group-by--having">6.2.4 group by + having</h3>
<p>having &#x6761;&#x4EF6;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;&#x7528;&#x6765;&#x8FC7;&#x6EE4;&#x5206;&#x7EC4;&#x7ED3;&#x679C; </p>
<blockquote>
<p>having&#x4F5C;&#x7528;&#x548C;where&#x7C7B;&#x4F3C;&#xFF0C;&#x4F46;having&#x53EA;&#x80FD;&#x7528;&#x4E8E;group by &#x800C;where&#x662F;&#x7528;&#x6765;&#x8FC7;&#x6EE4;&#x8868;&#x6570;&#x636E;&#xFF0C;</p>
</blockquote>
<pre><code class="lang-sql">select gender,count(*) from students group by gender having count(*)&gt;2;
+--------+----------+
| gender | count(*) |
+--------+----------+
| &#x7537;     |        5 |
| &#x5973;     |        7 |
+--------+----------+
</code></pre>
<h3 id="625-group-by--with-rollup">6.2.5 group by + with rollup</h3>
<ol>
<li>with rollup&#x7684;&#x4F5C;&#x7528;&#x662F;&#xFF1A;&#x5728;&#x6700;&#x540E;&#x65B0;&#x589E;&#x4E00;&#x884C;&#xFF0C;&#x6765;&#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x8868;&#x4E2D;&#x8BE5;&#x5B57;&#x6BB5;&#x5BF9;&#x5E94;&#x7684;&#x64CD;&#x4F5C;&#x7ED3;&#x679C;&#xFF0C;&#x4E00;&#x822C;&#x662F;&#x6C47;&#x603B;&#x7ED3;&#x679C;&#x3002;</li>
</ol>
<pre><code class="lang-sql">select gender,count(*) from students group by gender with rollup;
+--------+----------+
| gender | count(*) |
+--------+----------+
| &#x7537;     |        5 
| &#x5973;     |        7 |
| &#x4E2D;&#x6027;   |        1 |
| &#x4FDD;&#x5BC6;   |        1 |
| NULL   |       14 |
+--------+----------+


select gender,group_concat(age) from students group by gender with rollup;
+--------+-------------------------------------------+
| gender | group_concat(age)                         |
+--------+-------------------------------------------+
| &#x7537;     | 29,59,36,27,12                            |
| &#x5973;     | 18,18,38,18,25,12,34                      |
| &#x4E2D;&#x6027;   | 33                                        |
| &#x4FDD;&#x5BC6;   | 28                                        |
| NULL   | 29,59,36,27,12,18,18,38,18,25,12,34,33,28 |
+--------+-------------------------------------------+
</code></pre>
<h2 id="63-&#x603B;&#x7ED3;">6.3 &#x603B;&#x7ED3;</h2>
<ol>
<li>group by &#x5173;&#x952E;&#x5B57;&#x80FD;&#x6839;&#x636E;1&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5B57;&#x6BB5;&#x5BF9;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5206;&#x7EC4;</li>
<li>group_concat&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x5C06;&#x6BCF;&#x4E2A;&#x5206;&#x7EC4;&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x6210;&#x5458;&#x7684;&#x6307;&#x5B9A;&#x4E2A;&#x5B57;&#x6BB5;&#x62FC;&#x63A5;&#x5728;&#x4E00;&#x884C;&#x4E2D;&#x663E;&#x793A;</li>
<li>&#x805A;&#x5408;&#x51FD;&#x6570;&#x5728;&#x548C; group by &#x7ED3;&#x5408;&#x4F7F;&#x7528;&#x65F6;, &#x7EDF;&#x8BA1;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x6BCF;&#x4E2A;&#x5206;&#x7EC4;</li>
<li>having &#x662F;&#x5BF9;&#x5206;&#x7EC4;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x6761;&#x4EF6;&#x8FC7;&#x6EE4;</li>
<li>with rollup&#x5728;&#x5206;&#x7EC4;&#x7ED3;&#x679C;&#x6700;&#x540E;&#x65B0;&#x589E;&#x4E00;&#x884C;&#x5B8C;&#x6210;&#x6C47;&#x603B;&#x663E;&#x793A;&#x3002;</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../day15/05.聚合函数.html" class="navigation navigation-prev " aria-label="Previous page: 聚合函数"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../day15/07.连接.html" class="navigation navigation-next " aria-label="Next page: 连接"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python高级 -->
</html>
